package com.bidevalution.controller.api;

import com.bidevalution.business.service.BeSectionEnterpriseService;
import com.bidevalution.dao.entity.BeSectionEnterprise;
import com.bidevalution.service.EntInfoService;
import com.bidevalution.support.context.Constants;
import com.qtp.common.constants.SystemConstants;
import com.qtp.core.model.Response;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * Created by glodon on 2019-09-05.
 */
@Log4j2
@RestController
@RequestMapping("/beSectionEnterprise")
@Api(tags = "标段投标单位信息")
public class BeSectionEnterpriseController extends BaseController {
    @Autowired
    private BeSectionEnterpriseService beSectionEnterpriseService;
    @Autowired
    EntInfoService entInfoService;

    /**
     * 技术标为暗标的，生成暗标号
     * @param sectionId 标段Id
     * @return
     */
    @PostMapping("/generateHideNumber")
    @ApiOperation(value = "技术标为暗标的，生成暗标号", response = String.class)
    @ApiImplicitParams(
    {
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "sectionId", value = "标段主键", required = true),
    })
    public Response<String> generateHideNumber(@RequestParam("sectionId")String sectionId) {
        try {
            return Response.ok(beSectionEnterpriseService.generateHideNumber(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail(e.getMessage());
        }
    }

    /**
     * 根据标段id获取投标单位列表
     *
     * @param sectionId
     * @return
     */
    @GetMapping("/queryEnterBySectionId")
    @ApiOperation(value = "根据标段id获取投标单位列表(2019-09-17-v1)", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取投标单位列表失败"),
    })
    public Response queryEnterBySectionId(@RequestParam("sectionId") String sectionId) {
        if (StringUtils.isBlank(sectionId)) {
            Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
        }
        try {
            return Response.ok(beSectionEnterpriseService.getEnterpriseNames(sectionId));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取投标单位列表失败", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }
	/**
     * 根据标段id，编号获取入围投标单位列表
     *
     * @param sectionId
     * @return
     */
    @GetMapping("/getPreEnterBidInfo")
    @ApiOperation(value = "根据标段id获取入围投标单位列表", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "sectionCode", value = "项目编号", dataType = "String", paramType = "query", required = true)
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取投标单位列表失败"),
    })
    public Response getPreEnterBidInfo(@RequestParam("sectionId") String sectionId, @RequestParam("sectionCode") String sectionCode) {
        if (StringUtils.isBlank(sectionId)) {
            Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
        }
        try {
            return entInfoService.getPreEnterBidInfo(sectionId, sectionCode);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取投标单位列表失败", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }
    /**
     * 新增入围单位
     *
     * @param sectionId
     * @return
     */
    @GetMapping("/addSectionEnterprise")
    @ApiOperation(value = "新增入围单位", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "enterpriseName", value = "企业名称", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "bidProjectID", value = "项目Id", dataType = "String", paramType = "query", required = true)
    })
    public Response addSectionEnterprise(@RequestParam("sectionId") String sectionId, @RequestParam("enterpriseName") String enterpriseName
            , @RequestParam("bidProjectID") String bidProjectID) {
        try {
            BeSectionEnterprise item = new BeSectionEnterprise();
            item.setSeqId(UUID.randomUUID().toString());
            item.setSectionId(sectionId);
            item.setPtEnterpriseId(UUID.randomUUID().toString());
            item.setCreateTime(new Date());
            item.setEnterpriseName(enterpriseName);
            item.setBidProjectid(bidProjectID);
            item.setVersion(SystemConstants.NUM_1);
            return beSectionEnterpriseService.addSectionEnterprise(item);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("获取投标单位列表失败", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }
    /**
     * 根据企业id获删除投标单位
     *
     * @param seqId
     * @return
     */
    @GetMapping("/delSectionEnterprise")
    @ApiOperation(value = "根据企业id获删除投标单位", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "seqId", value = "企业id", dataType = "String", paramType = "query", required = true),
    })
    @ApiResponses({
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_ONE, message = "参数不可以为空"),
            @ApiResponse(code = Constants.FIVE_HUNDRED_AND_TWO, message = "获取投标单位列表失败"),
    })
    public Response delSectionEnterprise(@RequestParam("seqId") String seqId) {
        if (StringUtils.isBlank(seqId)) {
            Response.fail("参数不可以为空", Constants.FIVE_HUNDRED_AND_ONE);
        }
        try {
            return beSectionEnterpriseService.delSectionEnterprise(seqId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Response.fail("删除失败！", Constants.FIVE_HUNDRED_AND_TWO);
        }
    }

    @PostMapping("/updateEnterpriseName")
    @ApiOperation(value = "修改入围企业的名称", response = BeSectionEnterprise.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "seqId", value = "企业id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "projectId", value = "项目Id", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "name", value = "企业名称", dataType = "String", paramType = "query", required = true)
    })
    public Response updateEnterpriseName(@RequestParam("sectionId") String sectionId, @RequestParam("seqId") String seqId
            , @RequestParam("name") String name, @RequestParam("projectId") String projectId) {
        try {
            return beSectionEnterpriseService.updateEnterpriseName(sectionId, projectId, seqId, name);
        } catch (Exception e) {
            log.error("修改入围单位名称失败", e);
            return Response.fail("修改入围单位名称失败");
        }
    }
}
